package co.yixiang.modules.system.rest;

import co.yixiang.modules.quartz.domain.QuartzJob;
import co.yixiang.modules.quartz.service.QuartzJobService;
import co.yixiang.modules.shop.domain.YxSystemConfig;
import co.yixiang.modules.shop.service.YxSystemConfigService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 认购信息推送机器人
 *
 * @author frq
 * @date 2020-08-10
 */
@Slf4j
@Api(tags = "机器人")
@RestController
@RequestMapping("/api/subscripInformaPush")
public class SubscripInformaPushController {
    @Autowired
    private YxSystemConfigService yxSystemConfigService;
    @Autowired
    private QuartzJobService quartzJobService;

    @ApiOperation("认购信息推送机器人")
    @GetMapping("/getSubscripInforma")
    public ResponseEntity getSubscripInforma() {
        YxSystemConfig investInfoMachineInterval = yxSystemConfigService.findByKey("invest_info_machine_interval");
        QuartzJob job = quartzJobService.findByBeanName("subscripInformaPushTimer");
        Map<String, Object> map = new HashMap<>();
        map.put("seconds", investInfoMachineInterval.getValue());
        map.put("isPause", job.getIsPause());
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @ApiOperation("开启/关闭推送机器人")
    @PostMapping("/pauseChange")
    public ResponseEntity pauseChange(@RequestBody String data) {
        //推送间隔
        int seconds = JSONObject.parseObject(data).getInteger("seconds");
        //推送开关
        boolean isPause = JSONObject.parseObject(data).getBoolean("isPause");
        Map<String, String> map = new LinkedHashMap<>();
        map.put("msg", "开启成功");
        QuartzJob job = quartzJobService.findByBeanName("subscripInformaPushTimer");
        if (job != null) {
            //* * 8-18 * * ?
            String cronExpression = job.getCronExpression();
            //定时器固定执行时间是在每日8-18点
            //大于1小时
            if (seconds >= 60 * 60) {
                //间隔每小时第几分钟第几秒开始执行
                cronExpression =  seconds % 3600 % 60 % 60 + " " + seconds % 3600 / 60 + " 8-18" + "/" + seconds / 3600 + " * * ?";
                //大于1分钟
            } else if (seconds >= 60) {
                //每小时第几分钟弟几秒开始执行
                cronExpression = seconds % 60 + " */" + seconds / 60 + " 8-18 * * ?";
                //大于1秒钟
            } else if (seconds >= 1) {
                //从每分钟第几秒开始启动
                cronExpression = "*/" + seconds + " * 8-18 * * ?";
            }
            job.setCronExpression(cronExpression);
            //状态：1暂停、0启用 页面传递的true是开启定时器，定时器开启需要设置isPause为false
            job.setIsPause(isPause == true ? false : true);
            quartzJobService.update(job);
            yxSystemConfigService.tenancePriceFactor("invest_info_machine_interval", String.valueOf(seconds));
            map.put("msg", isPause == true ? "成功开启认购信息推送" : "成功关闭开启认购信息推送");
        } else {
            map.put("msg", "配置认购信息推送失败,定时器不存在");
        }

        return new ResponseEntity(map, HttpStatus.OK);
    }

    public static void main(String[] args) {
        System.out.println(28800 / 3600 + "时" + 6000 % 3600 / 60 + "分" + 6000 % 3600 % 60 % 60 + "秒");
    }

}
